package com.xingyun.kingdee.erp.tools.param.model;

import com.xingyun.kingdee.erp.tools.common.Reflection;
import com.xingyun.kingdee.erp.tools.common.XyFunction;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

import java.util.List;
import java.util.Objects;

/**
 * @author lingo
 * @version v1.0
 * @date 2020/4/29 17:58
 * @Company www.xyb2b.com
 */
@Setter
@Getter
@ToString
public class FilterInfo {
    private LinkOpt opt;

    private String fieldName;

    private boolean nested = false;

    private List<FilterInfo> nestedInfo;

    private FilterType filterType;

    private Object[] values;

    FilterInfo(XyFunction function, boolean isOr, FilterType filterType, Object... values) {
        Objects.requireNonNull(function);
        String fieldName = Reflection.getMethodToFieldName(function);
        this.fieldName = fieldName;
        this.values = values;
        this.filterType = filterType;
        this.opt = isOr ? LinkOpt.OR : LinkOpt.AND;
    }

    private FilterInfo() {
    }

    public static FilterInfo getNestedOr(boolean isOr, List<FilterInfo> filterInfoList) {
        FilterInfo filterInfo = new FilterInfo();
        filterInfo.setNested(true);
        filterInfo.setOpt(isOr ? LinkOpt.OR : LinkOpt.AND);
        filterInfo.setNestedInfo(filterInfoList);
        return filterInfo;
    }

    public boolean isOr() {
        return this.opt.equals(LinkOpt.OR);
    }

    /**
     * 以 and 连接 还是以 or 连接
     */
    @SuppressWarnings("AlibabaEnumConstantsMustHaveComment")
    public static enum LinkOpt {
        /**
         * and
         */
        AND("AND"),

        /**
         * or
         */
        OR("OR");

        public String code;

        LinkOpt(String code) {
            this.code = code;
        }
    }
}
